package com.cloudspace.jindun.activity.Woyao;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.activity.MainActivity;
import com.cloudspace.jindun.activity.WdActivity;
import com.cloudspace.jindun.model.UserInfoResult;
import com.cloudspace.jindun.network.url.YXHttpUrl;
import com.cloudspace.jindun.user.UserSessionManager;
import com.cloudspace.jindun.utils.ToastMaker;
import com.google.gson.Gson;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;

public class CellActivitye extends CustomTitleBaseActivity {
    @Bind(R.id.back)
    ImageView mBack;
    @Bind(R.id.cell_et_xgmm)
    EditText mCellEtXgmm;
    @Bind(R.id.call_bt)
    Button mCallBt;
    private String mCall;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cell_activitye, false);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.back, R.id.call_bt})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.call_bt:
                if (mCellEtXgmm.getText().toString() != null) {
                    mCall = mCellEtXgmm.getText().toString();
                    requestData(YXHttpUrl.WD_GPHONENAME_URI, UserSessionManager.getInstance().getUserId(), mCall);
                }else {
                    ToastMaker.showToastShort(R.string.phone_number_input_error_tip);
                }
                break;
        }
    }

    private void requestData(String url,String userId,String userPhone) {
        OkHttpUtils.post()
                .url(url)
                .addParams("userId",userId)
                .addParams("userPhone",userPhone)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        ToastMaker.showToastShort(R.string.generic_error);
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        Gson gson = new Gson();
                        UserInfoResult result = gson.fromJson(response, UserInfoResult.class);

                        if (result != null && result.status == 200) {
                            UserSessionManager.updateUserInfo(result.data);
                            ToastMaker.showToastShort(R.string.modify_success);

                            String sign = getIntent().getStringExtra("sign");
                            if (sign.equals("wd")) {
                                Intent intent = new Intent();
                                intent.setClass(CellActivitye.this, WdActivity.class);
                                startActivity(intent);
                            } else {
                                Intent intent = new Intent();
                                intent.setClass(CellActivitye.this, MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                startActivity(intent);
                            }

                            finish();
                        } else {
                            ToastMaker.showToastShort(R.string.set_phone_number_error);
                        }
                    }
                });
    }
}
